/***************************************************************************
*  Normalflea.cpp
*
*  July 2007
*  Copyright  2007  RuijinZheng,China
*  Email mebetop@126.com
****************************************************************************/

/*
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */
#include "Normalflea.hpp"

 Normalflealayer *Normalflealayer::Getonlyinstance(const old_connect *origconnect,
         const old_recv *origrecv,const old_send *origsend,const old_recvfrom *origrecvfrom,
         const old_sendto *origsendto){
        static Normalflealayer normalflea(origconnect,origrecv,origsend,origrecvfrom,origsendto);

        return  &normalflea;
}

const Proxyserver *Normalflealayer::Getproxyserver(const vector<Proxyserver> &availableserver){ 
        if(availableserver.size()<1){
              return NULL;
         }
     //   srand(time(0));
       // return &availableserver.at(rand()%(availableserver.size()));
        return &availableserver.at(0);
}
int Normalflealayer::Connectflea(const int &sockfd,const  sockaddr *serv_addr,const vector<Proxyserver> &availableserver){
        int ret=-1;
        bool actionresult=false;
        const Proxyserver * proxyserver = Getproxyserver(availableserver);
        if(proxyserver!=NULL){
              ret = Connecttoserver(sockfd,proxyserver->sServername,proxyserver->iPort);
         }
        else{
        	   Errordeliver("no suitable proxy server provied","please edit proxy server list solve this problem");
        	   return -1;
         }
        if(ret<0){
        	Errordeliver("failed to connect to proxy server",strerror(errno));
        	return ret;
        }
        if(proxyserver->sType=="Socks4"){
        	   actionresult=Actsocks4proxy(sockfd,*((const sockaddr_in *)(serv_addr)));
         }
       else if(proxyserver->sType=="Socks4a"){
    	      actionresult=Actsocks4aproxy(sockfd,*((const sockaddr_in *)(serv_addr)));
         }
        else if(proxyserver->sType=="Socks5"){
           	if((actionresult=Prepareforsocks5proxy(sockfd,proxyserver->sUser,proxyserver->sPasswd))==true){
           		 actionresult=Actsocks5proxy(sockfd,*((const sockaddr_in *)(serv_addr)));
           	}else{
           		   return -1;
            	}
           	
         }
        else if(proxyserver->sType=="Http1.1") {
        	   actionresult=Acthttpproxy(sockfd,*((const sockaddr_in *)(serv_addr)),
             proxyserver->sUser,proxyserver->sPasswd);
         }
        if(actionresult==true){
             return ret;
        }else{
        	   return -1;
        }

}

Normalflealayer::Normalflealayer(const old_connect *origconnect,const old_recv *origrecv,
        const old_send *origsend,const old_recvfrom *origrecvfrom,const old_sendto *origsendto):
        Lowlayer(origconnect,origrecv,origsend,origrecvfrom,origsendto)
{ 
}
